package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;
    @Reference
    private ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        try {
            Map list = memberService.getMemberReport();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        try {
            List<Map<String,Object>> list = orderService.getSetmealReport();

            //遍历list,获取其中的数据
            //创建一个Map
            Map<String,Object> map = new HashMap<>();
            map.put("setmealCount",list);

            //遍历list集合，获取其中的name
            List<String> setmealNames = new ArrayList<>();
            for (Map<String,Object> setmeal : list) {
                String name = (String) setmeal.get("name");
                //将获取的setmeal套餐名字放入到setmealNames中
                setmealNames.add(name);
            }

            map.put("setmealNames",setmealNames);

            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            Map<String,Object> map = reportService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String,Object> map = reportService.getBusinessReportData();

        String reportDate = (String) map.get("reportDate");
        Long todayNewMember = (Long) map.get("todayNewMember");
        Long totalMember = (Long) map.get("totalMember");
        Long thisWeekNewMember = (Long) map.get("thisWeekNewMember");
        Long thisMonthNewMember = (Long) map.get("thisMonthNewMember");
        Long todayOrderNumber = (Long) map.get("todayOrderNumber");
        Long todayVisitsNumber = (Long) map.get("todayVisitsNumber");
        Long thisWeekOrderNumber = (Long) map.get("thisWeekOrderNumber");
        Long thisWeekVisitsNumber = (Long) map.get("thisWeekVisitsNumber");
        Long thisMonthOrderNumber = (Long) map.get("thisMonthOrderNumber");
        Long thisMonthVisitsNumber = (Long) map.get("thisMonthVisitsNumber");
        List<Map> hotSetmeal = (List<Map>) map.get("hotSetmeal");

        //获取文件所在的真实路径
        String filePath = request.getSession().getServletContext().getRealPath("template")+File.separator+"report_template.xlsx";       //与xlsx建立连接
        XSSFWorkbook wk = new XSSFWorkbook(new FileInputStream(new File(filePath)));

        //获取第一个sheet文件
        XSSFSheet sheet = wk.getSheetAt(0);

        //设置日期数据
        XSSFRow row = sheet.getRow(2);
        row.getCell(5).setCellValue(reportDate);
        //设置新增会员数
        row = sheet.getRow(4);
        row.getCell(5).setCellValue(todayNewMember);
        //设置总会员数
        row.getCell(7).setCellValue(totalMember);
        //设置本周会员数
        row = sheet.getRow(5);
        row.getCell(5).setCellValue(thisWeekNewMember);
        //设置本月新增会员数
        row.getCell(7).setCellValue(thisMonthNewMember);

        //设置今日预约数
        row = sheet.getRow(7);
        row.getCell(5).setCellValue(todayOrderNumber);
        //设置今日到诊数
        row.getCell(7).setCellValue(todayVisitsNumber);

        //设置本周预约数
        row = sheet.getRow(8);
        row.getCell(5).setCellValue(thisWeekOrderNumber);
        row.getCell(7).setCellValue(thisWeekVisitsNumber);

        //设置本月预约数
        row = sheet.getRow(9);
        row.getCell(5).setCellValue(thisMonthOrderNumber);
        row.getCell(7).setCellValue(thisMonthVisitsNumber);

        //遍历List集合
        //设置行数
        int rowNum = 12;

        if(hotSetmeal != null) {
            for (Map setmeal : hotSetmeal) {
                //获取其中的数据
                String name = (String) setmeal.get("name");
                Long setmeal_count = (Long) setmeal.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) setmeal.get("proportion");
                String remark = (String) setmeal.get("remark");

                row = sheet.getRow(rowNum++);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmeal_count);
                row.getCell(6).setCellValue(proportion.doubleValue());
                row.getCell(7).setCellValue(remark);
            }
        }

        //获取输出流
        OutputStream os = response.getOutputStream();
        //设置响应头信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("content-Disposition","attachment;filename=report.xlsx");
        wk.write(os);

        //刷新流
        os.flush();
        //关闭流
        os.close();
        wk.close();
    }
}
